package com.zzptc.sky.bdsjws.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.socks.library.KLog;
import com.zzptc.sky.bdsjws.MyApplication;
import com.zzptc.sky.bdsjws.services.IVersionUpdate;
import com.zzptc.sky.bdsjws.services.MyDownLoadService;
import com.zzptc.sky.bdsjws.utils.MyUtil;
import com.zzptc.sky.bdsjws.vo.VersionInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/5/22 0022.
 */

public class CheckUpdateBroadcast extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, final Intent intent) {
        KLog.i("---","需要更新0.0！");
        SharedPreferences sharedPreferences= MyUtil.getSharedPre();//获取共享首选项
        String olddate=sharedPreferences.getString("checkDate",null);
        Date newdate=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String newdateForment=simpleDateFormat.format(newdate);
        //计算日期差
        try{
            //
            Date checkdate=null;
            if(olddate!=null){
              checkdate=simpleDateFormat.parse(olddate)  ;
            }
        if(olddate==null||"".equals(olddate)){
            //表示是第一次安装  将当前的日期存储在共享首选项
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString("checkDate",newdateForment);//存入之前要将Date转成String
            editor.commit();
        }else {
            //计算日期差
            int days=MyUtil.daysBetween(checkdate,newdate);
            if(days>=7){
                //访问服务器  看看有没有新的版本号
                Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl("http://10.0.2.2:8080/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                IVersionUpdate versionUpdate=retrofit.create(IVersionUpdate.class);//创建接口对象
                Call<VersionInfo> versionInfoCall=versionUpdate.getVersion();
                //将当前的日期存储在共享首选项 作为上次更新的日期
                versionInfoCall.enqueue(new Callback<VersionInfo>() {
                    @Override
                    public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                        VersionInfo versionInfo=response.body();
                        int newversioncode=versionInfo.getVersioncode();
                        int oldeversioncode=MyUtil.getVersionCode(MyApplication.getContext());
                        if (newversioncode>oldeversioncode){
                            //进行强制更新
                            Intent intent1=new Intent();
                            intent1.setClass(MyApplication.getContext(), MyDownLoadService.class);
                            intent1.putExtra("apkurl",versionInfo.getApkurl());
                            MyApplication.getContext().startService(intent1);
                        }
                    }

                    @Override
                    public void onFailure(Call<VersionInfo> call, Throwable t) {

                    }
                });
            }else {

            }
        }
    }catch (ParseException e){
            e.printStackTrace();
        }
    }
}
